package com.brillilab.service.core.team.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.lab.LabMemberMapper;
import com.brillilab.dao.mapper.team.TeamMapper;
import com.brillilab.dao.mapper.team.TeamMemberMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.enums.team.TeamMemberStateEnum;
import com.brillilab.domain.po.lab.LabMember;
import com.brillilab.domain.po.team.Team;
import com.brillilab.domain.po.team.TeamMember;
import com.brillilab.domain.vo.lab.LabMemberInfo;
import com.brillilab.domain.vo.team.TeamSelectorVo;
import com.brillilab.domain.vo.team.TeamVo;
import com.brillilab.service.core.team.ITeamService;
import com.brillilab.service.logic.lab.LabMemberLogic;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * 研究团队
 *
 * @author WuMenghao
 * @since 2019-03-02
 */
@Service
@Transactional
public class TeamServiceImpl implements ITeamService {

    @Resource
    private TeamMapper teamMapper;
    @Resource
    private TeamMemberMapper teamMemberMapper;
    @Resource
    private LabMemberMapper labMemberMapper;


    @Override
    public List<TeamVo> selectTeamByLabId(Long labId,Long ownerId) {
        Assert.isTrue(labId != null, "参数缺失！");
        return teamMapper.selectTeamByLabId(labId,ownerId);
    }

    public List<LabMemberInfo> selectLabMemberInfo(List<Long> LabMemberIdList) {
        Assert.isTrue(LabMemberIdList != null, "参数缺失！");
        return labMemberMapper.selectLabMemberInfo(LabMemberIdList);
    }

    public List<LabMemberInfo> selectLabMemberInfoByLabId(Long labId) {
        Assert.isTrue(labId != null, "参数缺失！");
        return labMemberMapper.selectLabMemberInfoByLabId(labId);
    }

    @Override
    public boolean insert(Team team) {
        Assert.notNull(team, "参数缺失！");
        return teamMapper.insert(team) > 0;
    }

    @Override
    public Team selectOneByIdAndLabIdAndIsDelete(Long id, Long labId, Integer isDelete) {
        Assert.isTrue(id != null && labId != null && isDelete != null, "参数缺失！");
        return teamMapper.selectOne(
                new LambdaQueryWrapper<Team>()
                        .eq(Team::getId, id)
                        .eq(Team::getLabId, labId)
                        .eq(Team::getIsDelete, isDelete));
    }

    @Override
    public Team selectOneByLeaderId(Long leaderId) {
        Assert.notNull(leaderId, "参数缺失！");
        return teamMapper.selectOne(new LambdaQueryWrapper<Team>()
                .eq(Team::getLeaderId, leaderId)
                .eq(Team::getIsDelete, IsDeleteEnum.NO.getValue()));
    }

    @Override
    public boolean updateTeamIsDelete(Long teamId) {
        Assert.isTrue(teamId != null, "参数缺失！");
        return teamMapper.updateTeamIsDelete(teamId) > 0;
    }

    @Override
    public Team selectOneByLabIdAndTeamId(Long labId, Long teamId) {
        Assert.isTrue(labId != null && teamId != null, "参数缺失！");
        return teamMapper.selectOne(new LambdaQueryWrapper<Team>()
                .eq(Team::getId, teamId)
                .eq(Team::getLabId, labId)
                .eq(Team::getIsDelete, IsDeleteEnum.NO.getValue()));
    }

    @Override
    public boolean updateById(Team team) {
        Assert.notNull(team, "参数缺失！");
        return teamMapper.updateById(team) > 0;
    }

    public Long selectLeaderIdByLabMemberId(Long labMemberId){
        return teamMapper.selectLeaderIdByLabMemberId(labMemberId);
    }

	@Override
	public Team selectByLabMemberId(Long labMemberId) {
		return teamMapper.selectByLabMemberId(labMemberId);
	}

    @Override
    public List<TeamSelectorVo> getTeamSelector(Long labId) {
        return teamMapper.selectTeamSelector(labId);
    }

    @Override
    public Team selectById(Long teamId) {
        return teamMapper.selectById(teamId);
    }

    @Override
    public List<Long> getTeamLabMemberIds(Long labMemberId) {
        TeamMember teamMember=teamMemberMapper.selectOne(
                new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getLabMemberId,labMemberId)
                        .eq(TeamMember::getState,TeamMemberStateEnum.IN.getState()));
        if(teamMember!=null){
            return teamMemberMapper.getTeamLabMemberIds(teamMember.getTeamId());
        }else {
            LabMember labMember=labMemberMapper.selectById(labMemberId);
            return teamMemberMapper.getNoTeamLabMemberIds(labMember.getLabId());
        }
    }
}
